<script lang="ts" setup>
    import { House, ArrowRight, List } from "@element-plus/icons-vue"
    import { ElMessage } from 'element-plus'
    import { reactive } from 'vue'
    import deptStore from '../store/DeptStore'
    import { onMounted } from "vue";
    import { Dept } from "../model/index";
    import axios from "../request/index";
    import { useRouter } from "vue-router";

    const router = useRouter();
    const store = deptStore();

    let dpno = store.getDeptno;
    let dept:Dept =  new Dept(0,'',0);;

    let data = reactive({
        "dept": dept,
    });

    const getData = function (){
        axios({
            url: `/dept.do?deptno=${dpno}`,
            method: "GET"
        }).then((res)=>{
            //请求成功
            console.log(dpno);
            data.dept = res.data;
            console.log(data.dept);

        }).catch((err)=>{
            //请求失败
            console.log(err);
        })
    }

    const updateDept = function (){
        axios({
            url: `/dept.do`,
            method: "POST",
            data: data.dept
        }).then((res)=>{
            //请求成功
            console.log(res.data);
            console.log(data.dept);
            router.push('/home/deptInfo')

        }).catch((err)=>{
            //请求失败
            ElMessage.error('更新数据失败')
            console.log(err);
        })
    }

    //页面加载时会自动执行
    onMounted(() => {
        getData();
    });

</script>

<template>
  <el-container id="mainView">
    <el-header style="background-color: #272822; color: #E5E5E5; line-height: 60px; font-size: 27px;">修改部门信息</el-header>
    <el-footer style="height: 30px; line-height: 30px; background-color: #4E5246; border-bottom:1px solid #7A7A7A; border-top:1px solid #7A7A7A;">
      <el-breadcrumb :separator-icon="ArrowRight" style="line-height: 30px;">
        <el-breadcrumb-item :to="{ path: '/home/homepage' }"><el-icon><House /></el-icon>首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/home/deptInfo' }">部门信息管理</el-breadcrumb-item>
        <el-breadcrumb-item>修改部门信息</el-breadcrumb-item>
      </el-breadcrumb>
    </el-footer>
    <el-main style="padding-bottom: 0%;">
      
      <el-card style="width: 100%; line-height: 55px;">
        <div class="card">
            <span style="color: #E5E5E5; line-height: 30px;">
                <el-icon><List /></el-icon>
                修改部门信息
            </span> <el-divider style="margin-bottom: 20px; margin-top: 0px;" />
            <div class="form_item">
                <el-form
                    style="width: 600px; "    
                    status-icon
                    label-width="auto"
                    class="demo-ruleForm"
                >
                    <el-form-item label="部门编号" >
                    <el-input v-model="data.dept.deptno" />
                    </el-form-item>
                    <el-form-item label="部门名称" required>
                    <el-input v-model="data.dept.dname" />
                    </el-form-item>
                    <el-form-item label="地区" required>
                    <el-input v-model="data.dept.locid" />
                    </el-form-item>
                    <div style="display: flex; justify-content: center;"> 
                        <el-form-item>
                        <el-button type="primary" @click="updateDept">
                            提交
                        </el-button>
                        </el-form-item>
                    </div>
                </el-form>
            </div>
        </div>
      </el-card>
     
    </el-main>
  </el-container>
</template>
  
<style scoped>
  #mainView{
    height: 100%;
    width: 100%;
  }
  
  .form_item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
  }
  
</style>
  